package model;

import java.util.ArrayList;

public class C {

	public static CalcList calcList= new CalcList();
	static String theme="";
	
	public static double calc(String term, double value1, double value2) {
		ArrayList valueList=new ArrayList();
		valueList.add(value1);
		valueList.add(value2);
		return calc(term, valueList, "").toDouble();
	}
	
	public static int calc(String id, String term, int value1, int value2) {
		ArrayList<Integer> valueList=new ArrayList<Integer>();
		valueList.add(value1);
		valueList.add(value2);
		return speedCalc(id, term, valueList);
	}
	
	
	public static double calc(String term, double value1, double value2, double value3) {
		ArrayList valueList=new ArrayList();
		valueList.add(value1);
		valueList.add(value2);
		valueList.add(value3);
		return calc(term, valueList, "").toDouble();
	}
	
	public static int calc(String id, String term, int value1, int value2, int value3) {
		ArrayList<Integer> valueList=new ArrayList<Integer>();
		valueList.add(value1);
		valueList.add(value2);
		valueList.add(value3);
		return speedCalc(id, term, valueList);
	}
	
	public static double calc(String term, double value1, double value2, double value3, double value4) {
		ArrayList valueList=new ArrayList();
		valueList.add(value1);
		valueList.add(value2);
		valueList.add(value3);
		valueList.add(value4);
		return calc(term, valueList, "").toDouble();
	}
	
	public static int calc(String id, String term, int value1, int value2, int value3, int value4) {
		ArrayList<Integer> valueList=new ArrayList<Integer>();
		valueList.add(value1);
		valueList.add(value2);
		valueList.add(value3);
		valueList.add(value4);
		return speedCalc(id, term, valueList);
	}

	
	/**
	 * this method should not be be used, instead use calc(String id, String term, int value1, int value2). its a little bit faster 
	 * @param term A term like x=y+z
	 * @param value1
	 * @param value2
	 * @return
	 */
	public static int calc(String term, int value1, int value2) {
		ArrayList<Integer> valueList=new ArrayList<Integer>();
		valueList.add(value1);
		valueList.add(value2);
		String id = StringAnalyser.getIdInString(term);
		return speedCalc(id, term, valueList);
	}

	private static int speedCalc(String id, String term, ArrayList<Integer> intValueList) {
		//Boolean correctTerm=checkTerm(term);
		CalcItem calcItem = calcList.findItemById(id);
		if (calcItem==null) {
			//TODO optional theme angeben im header
			calcItem=calcList.addItem(id+"="+term, "", theme);
		}
		return calcItem.speedCalc(intValueList);
	}
	
	/**
	 * calculates term with given values in valueList
	 * @param term something like x=a+b*(c^d)
	 * @param valueList a list like [7,9,3,0]
	 * @param descr a description like "The original add term"
	 * @return in this case: 7+9*(3^0)
	 */
	public static Value calc(String term, ArrayList valueList, String descr) {
		//Boolean correctTerm=checkTerm(term);
		String id = StringAnalyser.getIdInString(term);
		CalcItem calcItem = calcList.findItemById(id);
		if (calcItem==null) {
			//TODO optional theme angeben im header
			calcItem=calcList.addItem(term, descr,theme);
		}
		return calcItem.calc(valueList);
	}

	/**
	 * use this method to return a value calculated by the input term
	 * @param term something like "x=y+z"
	 * @param value1 a number like 1,5 or "1,5" etc
	 * @param value2 a number like 1,5 or "1,5" etc
	 * @return a value that can be casted in a needed type
	 */
	public static Value calc(String term, Object value1, Object value2) {
		ArrayList valueList=new ArrayList<Integer>();
		valueList.add(value1);
		valueList.add(value2);
		return calc(term, valueList, "");
	}
	
}
